﻿/*
Copyright © 2011 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;

namespace Simple.Storage.Api
{
    /// <summary>
    /// The default implementation of the <see cref="IPropertyDefinition"/> interface.
    /// </summary>
    [System.Diagnostics.DebuggerDisplay("DefaultPropertyDefinition [Type:{Type}]")]
    public class DefaultPropertyDefinition : IPropertyDefinition
    {
        /// <summary>
        /// The type of the property.
        /// </summary>
        private readonly Type type;

        /// <summary>
        /// The function used to extract the value of the property.
        /// </summary>
        private readonly Func<object, object> get;

        /// <summary>
        /// The function used to define the value of the property.
        /// </summary>
        private readonly Action<object, object> set;

        /// <summary>
        /// Initializes a new instance of the DefaultPropertyDefinition class.
        /// </summary>
        /// <param name="type">
        /// The type of the property.
        /// </param>
        /// <param name="get">
        /// The function used to extract the value of the property.
        /// </param>
        /// <param name="set">
        /// The function used to define the value of the property - could be <c>null</c>.
        /// </param>
        public DefaultPropertyDefinition(Type type, Func<object, object> get, Action<object, object> set)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            else if (get == null)
            {
                throw new ArgumentNullException("get");
            }

            this.type = type;
            this.get = get;
            this.set = set;
        }

        /// <summary>
        /// Gets the type of the property.
        /// </summary>
        public Type Type
        {
            get { return this.type; }
        }

        /// <summary>
        /// Gets a value indicating whether the a in-place serialization should
        /// be used for this property (no use of the <see cref="IPropertyDefinition.SetValue"/>).
        /// </summary>
        public bool RequireInPlace
        {
            get { return this.set == null; }
        }

        /// <summary>
        /// Extracts the value of the property from a specific object.
        /// </summary>
        /// <param name="instance">The instance of the parent object.</param>
        /// <returns>
        /// The value of the property in <paramref name="instance"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="instance"/> parameter is <c>null</c>.
        /// </exception>
        public object GetValue(object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            return this.get(instance);
        }

        /// <summary>
        /// Defines the value of the property for a specific object.
        /// </summary>
        /// <param name="instance">The instance of the parent object.</param>
        /// <param name="value">The new value for the property.</param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="instance"/> parameter is <c>null</c>.
        /// </exception>
        public void SetValue(object instance, object value)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            else if (this.set == null)
            {
                // TODO: message
                throw new NotSupportedException();
            }

            this.set(instance, value);
        }
    }
}
